function floating_window(backdropId, boxId) {
	$('#' + backdropId + ', #' + boxId).animate({
		'opacity' : '.50'
	}, 300, 'linear');
	$('#' + boxId).animate({
		'opacity' : '1.00'
	}, 300, 'linear');
	$('#' + backdropId + ', #' + boxId).css('display', 'block');
}

function close_box(backdropId, boxId) {
	$('#' + backdropId + ', #' + boxId).animate({
		'opacity' : '0'
	}, 300, 'linear', function() {
		$('#' + backdropId + ', #' + boxId).css('display', 'none');
	});
}

function showDiscount() {
	var tbodyShoppingCart = $('#tbodyShoppingCart');
	var discountRow = $('<tr></tr>').attr('id', 'discountRow');
	var discountCell = $('<td></td>').attr('colspan', 2).append('<strong>Total after discount:</strong>').show();
	var total = getShoppingCartTotal().toFixed(2);
	var discountedTotal = (total * 0.9).toFixed(2);
	var discountedTotalCell = $('<td></td>').attr('id', 'discountedTotalCell').append('$' + discountedTotal).show();
	discountRow.append(discountCell);
	discountRow.append(discountedTotalCell);
	discountRow.append($('<td></td>'));
	tbodyShoppingCart.append(discountRow);
	var purchaseBtn = $('#purchaseBtn');
	var text = purchaseBtn.text();
	text = text.replace(total, discountedTotal);
	purchaseBtn.text(text);
}

function getShoppingCartTotal() {
	var shoppingCartTotalValue = $('#shoppingCartTotalValue').text();
	shoppingCartTotalValue = shoppingCartTotalValue.replace('$', '');
	return parseFloat(shoppingCartTotalValue);
}

function hideDiscount() {
	var discountRow = $('#discountRow');
	discountRow.remove();
	var purchaseBtn = $('#purchaseBtn');
	var text = purchaseBtn.text();
	var total = getShoppingCartTotal().toFixed(2);
	var discountedTotal = (total * 0.9).toFixed(2);
	text = text.replace(discountedTotal, total);
	purchaseBtn.text(text);
}

function showNutritionDetailsImg(userCalories, shoppingCartCalories) {
	var img = $('#nutritionDetailsImg');
	var src = '/public/img/smiley.png';
	if (userCalories != null && shoppingCartCalories != null) {
		userCalories = parseFloat(userCalories);
		shoppingCartCalories = parseFloat(shoppingCartCalories);	
		// daily calories divided by 3 meals
		var mealCalories = userCalories / 3;		
		// if calories in the shopping cart are more than meal calories
		if (mealCalories < shoppingCartCalories) {
			// if at most 10% more -> neutral face
			if (mealCalories * 1.1 > shoppingCartCalories) {
				src = src.replace('smiley', 'neutral-face');
			}
			// sad face
			else {
				src = src.replace('smiley', 'sad');
			}
		} 
		// meal calories more than in shopping cart
		else {
			src = '/public/img/smiley.png';
		}
	}
	img.attr('src', src);
}
